package com.since.algorithm.sort;

import java.util.Arrays;

/**
 * Created by Sincerity on 2020/5/13.
 * 描述：八大排序
 * 1. 冒泡排序{@link #bubbleSort(int[])}
 */
class BubbleSort {


    public static void main(String[] args) {
        int[] array = {3, 9, -1, 10, 20};
        BubbleSort.bubbleSort(array);
    }

    /**
     * 冒泡排序
     *
     * @param arr 需要排序的数组
     */
    public static void bubbleSort(int[] arr) {
        int temp;
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            if (!flag) {
                break;
            } else {
                flag = false;
            }
            System.out.print(Arrays.toString(arr) + "\n");
        }
        System.out.print(Arrays.toString(arr));
    }
}
